package com.example.recordingpen_datacollection.controller;


import com.alibaba.fastjson.JSON;
import com.codingcube.simpleauth.limit.annotation.SimpleLimit;
import com.example.recordingpen_datacollection.Utils.SizeListUtil.LimitedSizeList;
import com.example.recordingpen_datacollection.Utils.http.Messages;
import com.example.recordingpen_datacollection.config.constant.ConstantName;
import com.example.recordingpen_datacollection.config.contract.annotation.RequestLimit;
import com.example.recordingpen_datacollection.entity.Query.AiconsultQue;
import com.example.recordingpen_datacollection.entity.Vo.AISumUp;
import com.example.recordingpen_datacollection.entity.Vo.BDAISumUp;
import com.example.recordingpen_datacollection.redisService.RedisService;
import com.example.recordingpen_datacollection.result.RLIST;
import com.example.recordingpen_datacollection.service.AiconsultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2024-05-23
 */
@RestController
@Api(tags = "问答测试")
@RequestMapping("/aiconsult")
public class AiconsultController {

    @Autowired
    private AiconsultService aiconsultService;
    @Autowired
    private RedisService redisService;

    @ApiOperation(value = "AI咨询")
    @PostMapping("/aiCounseling")
    public RLIST aiCounseling(@RequestBody BDAISumUp aiSumUp, HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 36 ){
            return RLIST.error().message("无权操作");
        }
        if(aiSumUp.getContent().length()>500){
            return RLIST.error().message("问题过长 小于500字");
        }
        Integer value = (Integer) redisService.getValue(token);


        String txtSumUp = aiconsultService.BDERNIE_Speed(aiSumUp, value);
        return RLIST.ok().data("data",txtSumUp);
    }

    @ApiOperation(value = "查询聊天情况 设备")
    @PostMapping("/getaiconsult")
    public RLIST getAiconsult(@RequestBody AiconsultQue aiconsultQue, HttpServletRequest request){



        String token = request.getHeader("token");
        if( token.length() != 36 ){
            return RLIST.error().message("无权操作");
        }
        Integer value = (Integer) redisService.getValue(token);

        if ((aiconsultQue.getPageNo() == null || aiconsultQue.getPageNo() <=0) ){
            aiconsultQue.setPageNo(1);
        }
        if ((aiconsultQue.getPageSize() == null || aiconsultQue.getPageSize() <=0) ){
            aiconsultQue.setPageSize(10);
        }

        RLIST rlist = aiconsultService.getAiconsultPage(aiconsultQue,value,token.length());

        return rlist;
    }

//    @RequestLimit(count=5,time=10000)
    //10分钟内只允许访问5次，超过之后将会被禁止10分钟
//    @SimpleLimit(value = 5, seconds = 60, ban = 60)
    @ApiOperation(value = "测试接口")
    @PostMapping("/text")
    public RLIST text(@RequestBody BDAISumUp aiSumUp, HttpServletRequest request){
        return RLIST.ok().data("data",1010110);
    }
    @ApiOperation(value = "测试接口")
    @PostMapping("/text2")
    public RLIST text2(@RequestBody BDAISumUp aiSumUp, HttpServletRequest request){
        return RLIST.ok().data("data",1010110);
    }

    @ApiOperation(value = "AI咨询流式")
    @PostMapping("/aiStream")
    public String aiStream(@RequestBody BDAISumUp aiSumUp, HttpServletResponse rp, HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 36 ){
            String error = "data:{\"success\":\"false\",\"code\":\"400\",\"result\":\"No right to operate\"}";
//            return RLIST.error().message("无权操作");
            return error;
        }
        if(aiSumUp.getContent().length()>500){
            String error = "data:{\"success\":\"false\",\"code\":\"400\",\"result\":\"The problem is less than 500 words long\"}";
//            return RLIST.error().message("问题过长 小于500字");
            return error;
        }
        if(aiSumUp.getContent() == null || aiSumUp.getContent().isEmpty()){
            String error = "data:{\"success\":\"false\",\"code\":\"400\",\"result\":\"Content is not allowed to be empty\"}";
//            return RLIST.error().message("问题过长 小于500字");
            return error;
        }
        Integer value = (Integer) redisService.getValue(token);


         aiconsultService.BDERNIE_stream(aiSumUp, value, rp);
        return null;
    }



}

